 

*************************************************************************************************************
**************************************** I. SPATIAL DATA ****************************************************
*************************************************************************************************************

*** Bring in B and S data, and organise: 

clear all
set maxvar 20000
use "Table S6 dta", clear

split border, parse("-")
for any 1 2: replace borderX=upper(borderX) \ replace borderX="SOMALE" if borderX=="SOMALI" \ replace borderX="BENISHANGUL-GUMUZ" if borderX=="BENISHANGUL GUMUZ" \ replace borderX="HARRARI" if borderX=="HARARI" 
 g		border_1=border1 if border1 ==region_name
 g		border_2=border2 if border1 ==region_name 
replace border_1=border2 if border_1==""
replace border_2=border1 if border_2==""
compare region_name border_1
drop border border1 border2 border_1 
ren border 	bordering_region_name
g 			bordering_region="01" if bordering_region_name=="TIGRAY"
replace 	bordering_region="02" if bordering_region_name=="AFAR"
replace 	bordering_region="03" if bordering_region_name=="AMHARA"
replace 	bordering_region="04" if bordering_region_name=="OROMIA"
replace 	bordering_region="05" if bordering_region_name=="SOMALE"
replace 	bordering_region="06" if bordering_region_name=="BENISHANGUL-GUMUZ"
replace 	bordering_region="07" if bordering_region_name=="SNNP"
replace 	bordering_region="12" if bordering_region_name=="GAMBELLA"
replace 	bordering_region="13" if bordering_region_name=="HARRARI"
format 		bordering* %25s
format S B %10.2fc
tempfile spatial_data
save 	`spatial_data', replace



*************************************************************************************************************
************************************** II. Ag Sample Survey 1999-2000 ***************************************
*************************************************************************************************************

*** Organise each AgSS sub-dataset so as to rename variables, recode, bring into consistent form  
	
******************* 99-1: Contains basic demographics of holder
use "Table S6 dta1.dta", clear	

* Recode the values as appropriate:
g age_sq=age^2
recode sex 2=0
g 	    read_write=1  if edu~=1 & edu~=.
recode  read_write .=0
replace read_write=.  if edu==.
g 	    secorhigher=1 if edu>=5
recode  secorhigher .=0
replace secorhigher=. if edu==.
recode hh_size 99=.
g farms_crop  =1 if type==1 | type==3
g farms_livest=1 if type==2 | type==3
for any crop livest: recode farms_X .=0

* Rename vars and drop unneeded ones:
lab var read "the edu-status var was not 'illiterate'"
lab var sec  "the edu-status var showed 9th grade or higher"
lab var sex  "male dummy"
lab var age_sq "age squared"
lab var farms_crop   "type of agriculture: includes crop (either crop only or crop plus livestock)"
lab var farms_livest "type of agriculture: includes livestock (either livest. only or crop plus livest.)"
drop *edu* *type*

rename * d99_1_=
for any region region_name zone EA holder: ren d99_1_X X
tempfile 99_1_holder
save 	`99_1_holder', replace


********************* 99-2: For pure stands. Quantity of fertiliser; Usage of inputs
use "Table S6 dta2.dta", clear

* Recode and clean variables as appropriate:
for any use_p use_im use_f: recode X 2=0
recode  use_p 3/9=. 
g 	   use_fert_dap_P =1 if type ==2 | type ==3 
recode use_fert_dap  .=0 if use_fert_tot~=.
g		use_A_DSP_P=0
replace use_A_DSP=1 if use_imseed==1 | use_pest==1 | use_fert_dap ==1

*Outcomes
lab var use_A_DSP 	 "uses either DAP, improved seed, or pesticides"
lab var use_imseed   "uses improved seed"
lab var use_pest_dm  "uses pesticides (in the original dataset, pesticides were not further disaggregated)"
lab var use_fert_dap "uses DAP fertiliser"

rename * d99_2_=
for any region zone EA holder: ren d99_2_X X
foreach v of var * {
local l`v' : var lab `v'
if `"`l`v''"' == "" {
local l`v' "`v'"
}
}

label save using "Table S6 suppl do.do", replace
local listv
foreach v of var * {
local templocal : val lab `v'
if ("`templocal'" != "") {
local varlabel_`v' : val lab `v'
local listv "`listv' 		 `v'"
}
}
collapse (first) region zone EA (max) *use_*, by(holder)   

do "Table S6 suppl do.do"
foreach w of local listv {
cap label values `w' `varlabel_`w''
}
foreach  v of var * {
lab var `v' "`l`v''"
}
aorder
tempfile 99_2_holder
save 	`99_2_holder', replace


********************* 99-3: For mixed stands: Quantity of fertiliser; Usage of inputs; 
use "Table S6 dta3.dta", clear

* Recode and clean variables as appropriate:
for any use_f use_im use_p: recode X 2=0
g 		use_fert_dap_M =1 if type==2 | type==3 

recode use_fert_dap .=0 if use_fert_tot~=.
g 		use_A_DSP_M=0
replace use_A_DSP=1 if use_imseed==1 | use_pest==1 | use_fert_dap ==1

*Outcomes:
lab var use_A_DSP	  "uses either DAP, improved seed, or pesticides"
lab var use_imseed    "uses improved seed"
lab var use_pest_dm   "uses pesticides (in the original dataset, pesticides were not further disaggregated)"
lab var use_fert_dap  "uses DAP fertiliser"

rename * d99_3_=
for any region region_name zone EA holder: ren d99_3_X X
foreach v of var * {
local l`v' : var lab `v'
if `"`l`v''"' == "" {
local l`v' "`v'"
}
}
label save using "Table S6 suppl do.do", replace
local listv
foreach v of var * {
local templocal : value lab `v'
if ("`templocal'" != "") {
local varlabel_`v' : value lab `v'
local listv "`listv' `v'"
}
}
collapse (first) region region_name zone EA (max) *use_*, by(holder)   

do "Table S6 suppl do.do"
foreach w of local listv {
cap label values `w' `varlabel_`w''
}
foreach  v of var * {
lab var `v' "`l`v''"
}
aorder
tempfile 99_3_holder
save 	`99_3_holder', replace


******************************** 99-4: Land area
use "Table S6 dta4.dta", clear
rename * d99_4_=
for any region region_name zone EA holder: ren d99_4_X X

foreach v of var * {
local l`v' : var lab `v'
if `"`l`v''"' == "" {
local l`v' "`v'"
}
}
label save using "Table S6 suppl do.do", replace
local listv
foreach v of var * {
local templocal : 	 value label `v'
if ("`templocal'" != "") {
local varlabel_`v' : value label `v'
local listv "`listv' 			 `v'"
}
}
collapse (first) region region_name zone EA (sum) *area_sqm, by(holder)  
do "Table S6 suppl do.do"
foreach 		  w of local listv {
cap label values `w' `varlabel_`w''
}
foreach  v of var * {
lab var `v' "`l`v''"
}
aorder
tempfile 99_4_holder
save 	`99_4_holder', replace


**********************************************************************************************************
********************* III. COMBINE AgSS 1999-2000 MODULES AND SPATIAL DATA *******************************
**********************************************************************************************************

*** Combine the AgSS sub-datasets together:
#delimit;
				   use `99_1_holder', clear; 
merge 1:1 holder using `99_2_holder', update; drop _m;
merge 1:1 holder using `99_3_holder', update; drop _m;
merge 1:1 holder using `99_4_holder', update; drop _m;
for any 1 2 3 4: renpfix d99_X_; 

for any fert_dap imseed_dm pest_dm : egen  use_X     = rowmax(use_X_P	  use_X_M) \ drop use_X_P use_X_M;
									 egen  use_A_DSP = rowmax(use_A_DSP_P use_A_DSP_M); 
														 drop use_A_DSP_P use_A_DSP_M ;
									 egen number_DSP = rowtotal(use_fert_dap use_imseed_dm use_pest_dm);
#delimit cr

foreach v of var use_* farms_* {
recode `v' .=0   
}

*** Merge with spatial data, label decentralisation observations, etc.:
merge m:1 EA using `spatial_data' 
keep if _m==3
drop 	_m
g				 			 D=1 if 		  region=="01" | 		   region=="03" | 		    region=="04" | 			 region=="07"    
g 				   bordering_D=1 if bordering_region=="01" | bordering_region=="03" | bordering_region=="04" | bordering_region=="07"    
recode D 		   bordering_D (.=0) 
g policy_change=D~=bordering_D
replace S =-S if D==0
g abs_S=abs(S)
g Bsqu=B^2
g Bcub=B^3

egen segment=group(region_name bordering_region_name), label
tab	 segment, gen(A)
#delimit;
egen _A1 =rowmax(A1  A4 ); lab var _A1  "Afar Amhara segment";
egen _A2 =rowmax(A2  A13); lab var _A2  "Afar Oromia segment";
egen _A3 =rowmax(A3  A22); lab var _A3  "Afar Somale segment";
egen _A4 =rowmax(A5  A8 ); lab var _A4  "Amhara Beneshangul-Gumuz segment";
egen _A5 =rowmax(A6  A14); lab var _A5  "Amhara Oromia segment";
egen _A6 =rowmax(A7  A25); lab var _A6  "Amhara Tigray segment";
egen _A7 =rowmax(A9  A15); lab var _A7  "Beneshangul-Gumuz Oromia segment";
egen _A8 =rowmax(A10 A16); lab var _A8  "Gambella Oromia segment";
egen _A9 =rowmax(A11 A20); lab var _A9  "Gambella SNNP segment";
egen _A10=rowmax(A12 A17); lab var _A10 "Harari Oromia segment";
egen _A11=rowmax(A18 A21); lab var _A11 "Oromia SNNP segment";
egen _A12=rowmax(A19 A23); lab var _A12 "Oromia Somale segment";
egen _A13=rowmax(A24)	 ; lab var _A13 "Afar Tigray segment";
#delimit cr

egen zonn=group(zone)

forval 	  numb = 1/13 {
g 		_A`numb'_B 	   = _A`numb' * B
g 		_A`numb'_Bsqu  = _A`numb' * Bsqu
g 		_A`numb'_Bcub  = _A`numb' * Bcub
g 		_A`numb'_S	   = _A`numb' 		 * S
g 		_A`numb'_B_S   = _A`numb' * B    * S
g 		_A`numb'_Bsqu_S= _A`numb' * Bsqu * S
g 		_A`numb'_Bcub_S= _A`numb' * Bcub * S
}

lab var Bsqu "B-squared"
lab var Bcub "B-cubed"
sort policy segment B


****************************** Create groups of variables using locals, and label
#delimit;

local M4b_lin_newse "D
					 _A1     	 _A2	    _A3   	   _A4  	  _A5	  	 _A6  	    _A7   	   _A8  	  _A9 	 	 _A10	   	 _A11 	   	 _A12	 	 _A13	 	 
					 _A1_B   	 _A2_B	    _A3_B	   _A4_B	  _A5_B 	 _A6_B 	    _A7_B 	   _A8_B	  _A9_B	 	 _A10_B	   	 _A11_B	 	 _A12_B	  	 _A13_B	 	 
					 _A1_S   	 _A2_S	    _A3_S 	   _A4_S	  _A5_S		 _A6_S	    _A7_S	   _A8_S      _A9_S	 	 _A10_S	   	 _A11_S	 	 _A12_S	  	 _A13_S	 	 
					 _A1_B_S 	 _A2_B_S    _A3_B_S	   _A4_B_S	  _A5_B_S	 _A6_B_S	_A7_B_S	   _A8_B_S	  _A9_B_S	 _A10_B_S	 _A11_B_S	 _A12_B_S	 _A13_B_S   "; 
local M_b_squ_newse "_A1_Bsqu 	 _A2_Bsqu   _A3_Bsqu   _A4_Bsqu   _A5_Bsqu   _A6_Bsqu   _A7_Bsqu   _A8_Bsqu   _A9_Bsqu   _A10_Bsqu   _A11_Bsqu   _A12_Bsqu	 _A13_Bsqu  "; 	 
local M4b_squ_newse "_A1_Bsqu_S  _A2_Bsqu_S _A3_Bsqu_S _A4_Bsqu_S _A5_Bsqu_S _A6_Bsqu_S _A7_Bsqu_S _A8_Bsqu_S _A9_Bsqu_S _A10_Bsqu_S _A11_Bsqu_S _A12_Bsqu_S _A13_Bsqu_S";	

#delimit cr

aorder
lab var use_A_DSP	   "uses either DAP, improved seed, or pesticides"
lab var use_imseed_dm  "uses improved seed"
lab var use_pest_dm    "uses pesticides"
lab var use_fert_dap   "uses DAP fertiliser"
local hh_controls "sex read_write secorhigher age age_sq hh_size farms_livest area"


*************************************************************************************************************
******************************************* IV. REGRESSIONS *************************************************
*************************************************************************************************************


************************* A. RESTRICT SAMPLE SIZE FOR DIFFERENT SCENARIOS; INITIALISE REGRESSIONS

keep if policy_change==1
for any number_DSP use_A_DSP use_fert_dap use_imseed_dm use_pest_dm: ren X Y_X 
drop if region_n=="HARRARI" | region_n=="SOMALE" | bordering_region_n=="HARRARI" | bordering_region_n=="SOMALE"   
tempfile for_regression
save	`for_regression'


*** Initialise then run regressions:
#delimit;
keep if policy==1 & abs_S<=75000;
reg Y_number_DSP `M4b_lin_newse' `M_b_squ_newse' `M4b_squ_newse' `hh_controls', nocon vce(cluster zonn); outreg2 using "Table S6 results.xls", bdec(4) e(all) se label(insert) ctitle(ignore)   
sortvar( 	     `M4b_lin_newse' `M_b_squ_newse' `M4b_squ_newse') replace; 

use `for_regression', clear;
keep if policy==1 & abs_S<=75000;

foreach Y of var Y_use_A_DSP Y_number_DSP Y_use_fert_dap Y_use_pest_dm Y_use_imseed_dm {  ; 
local description: var label `Y';                                          
reg    `Y'   `M4b_lin_newse' `M_b_squ_newse' `M4b_squ_newse' `hh_controls', nocon vce(cluster zonn); outreg2 using "Table S6 results.xls", bdec(4) e(all) se label(insert) ctitle(`Y') 			
sortvar(     `M4b_lin_newse' `M_b_squ_newse' `M4b_squ_newse') append; 
};
#delimit cr

putexcel set "Table S6 mean values.xlsx", replace
putexcel A1 = "Uses modern inputs"
putexcel A2 = "No. of modern inputs used"
putexcel A3 = "Uses fertilizer"
putexcel A4 = "Uses pesticide"
putexcel A5 = "Uses improved seed"
sum Y_use_A_DSP 
return list
putexcel B1=`r(mean)' 
sum Y_number_DSP 
return list
putexcel B2=`r(mean)' 
sum Y_use_fert_dap 
return list
putexcel B3=`r(mean)' 
sum Y_use_pest_dm 
return list
putexcel B4=`r(mean)' 
sum Y_use_imseed_dm
return list
putexcel B5=`r(mean)' 


